from PySide6.QtCore import Signal, QRunnable, QThread


class TaskThreadSignals:
    success = Signal(object)
    faild = Signal()


class TaskThread(QRunnable):
    def __init__(self, func, *args, **kwargs):
        super().__init__()
        self.func = func
        self.args = args
        self.kwargs = kwargs
        self.signals = TaskThreadSignals()

    def run(self):
        res = self.func(*self.args, **self.kwargs)
        if res:
            self.signals.success.emit(res)
        else:
            self.signals.faild.emit()


class TaskWorker(QThread):
    success = Signal(object)
    faild = Signal()

    def __init__(self, func, *args, **kwargs):
        super().__init__()
        self.func = func
        self.args = args
        self.kwargs = kwargs

    def run(self):
        try:
            res = self.func(*self.args, **self.kwargs)
            self.success.emit(res)
        except Exception as e:
            self.faild.emit()
